home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / ddx / Xsun / sunCG6C.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-07  |  13.4 KB  |  520 lines

  1. /*-
  2.  * sunCG6C.c --
  3.  *    Functions to support the sun CG6 board as a memory frame buffer.
  4.  */
  5.  
  6. /****************************************************************/
  7. /* Modified from  sunCG4C.c for X11R3 by Tom Jarmolowski    */
  8. /****************************************************************/
  9.  
  10. /************************************************************
  11. Copyright 1987 by Sun Microsystems, Inc. Mountain View, CA.
  12.  
  13.                     All Rights Reserved
  14.  
  15. Permission  to  use,  copy,  modify,  and  distribute   this
  16. software  and  its documentation for any purpose and without
  17. fee is hereby granted, provided that the above copyright no-
  18. tice  appear  in all copies and that both that copyright no-
  19. tice and this permission notice appear in  supporting  docu-
  20. mentation,  and  that the names of Sun or MIT not be used in
  21. advertising or publicity pertaining to distribution  of  the
  22. software  without specific prior written permission. Sun and
  23. M.I.T. make no representations about the suitability of this
  24. software for any purpose. It is provided "as is" without any
  25. express or implied warranty.
  26.  
  27. SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS  SOFTWARE,
  28. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
  29. NESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE  LI-
  30. ABLE  FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  31. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,  DATA  OR
  32. PROFITS,  WHETHER  IN  AN  ACTION OF CONTRACT, NEGLIGENCE OR
  33. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
  34. THE USE OR PERFORMANCE OF THIS SOFTWARE.
  35.  
  36. ********************************************************/
  37.  
  38. #ifndef    lint
  39. static char sccsid[] = "@(#)sunCG6C.c    1.4 6/1/87 Copyright 1987 Sun Micro";
  40. #endif
  41.  
  42. #include    "sun.h"
  43. #include    <sys/types.h>
  44. #include "sys/fb.h"
  45. #include "sys/ioctl.h"
  46. #include "kernel/vmMach.h"
  47. #undef    MAP_NEW
  48.  
  49. #include    <sys/mman.h>
  50. #ifdef sprite
  51. #undef _MAP_NEW
  52. #endif
  53.  
  54. #ifndef    sprite
  55. #include    <pixrect/memreg.h>
  56. #include    <sundev/cg6reg.h>
  57. #include    <struct.h>
  58. #endif    sprite
  59. #include    "colormap.h"
  60. #include    "colormapst.h"
  61. #include    "resource.h"
  62.  
  63. #ifndef    sprite
  64. #define    CG6_HEIGHT    900
  65. #define    CG6_WIDTH    1152
  66.  
  67. typedef struct cg6c {
  68.     u_char cpixel[CG6_HEIGHT][CG6_WIDTH];    /* byte-per-pixel memory */
  69. } CG6, CG6Rec, *CG6Ptr;
  70.  
  71.  
  72. #define CG6_IMAGE(fb)        ((caddr_t)(&(fb)->cpixel))
  73. #define CG6_IMAGEOFF        ((off_t)0x0)
  74. #define CG6_IMAGELEN        (((CG6_HEIGHT*CG6_WIDTH + 8191)/8192)*8192)
  75. #endif    sprite
  76.  
  77. static int  sunCG6CScreenIndex;
  78.  
  79. /* XXX - next line means only one CG6 - fix this */
  80. static ColormapPtr sunCG6CInstalledMap;
  81.  
  82. extern int TellLostMap(), TellGainedMap();
  83.  
  84. static void
  85. sunCG6CUpdateColormap(pScreen, index, count, rmap, gmap, bmap)
  86.     ScreenPtr    pScreen;
  87.     int        index, count;
  88.     u_char    *rmap, *gmap, *bmap;
  89. {
  90.     fbcmap    sunCmap;
  91.  
  92.     sunCmap.index = index;
  93.     sunCmap.count = count;
  94.     sunCmap.red = &rmap[index];
  95.     sunCmap.green = &gmap[index];
  96.     sunCmap.blue = &bmap[index];
  97.  
  98. #ifdef SUN_WINDOWS
  99.     if (sunUseSunWindows()) {
  100.     static Pixwin *pw = 0;
  101.  
  102.     if (! pw) {
  103.         if ( ! (pw = pw_open(windowFd)) )
  104.         FatalError( "sunCG6CUpdateColormap: pw_open failed\n" );
  105.         pw_setcmsname(pw, "X.V11");
  106.     }
  107.     pw_putcolormap(
  108.         pw, index, count, &rmap[index], &gmap[index], &bmap[index]);
  109.     }
  110. #endif SUN_WINDOWS
  111.  
  112.     if (ioctl(sunFbs[pScreen->myNum].fd, FBIOPUTCMAP, &sunCmap) < 0) {
  113.     perror("sunCG6CUpdateColormap");
  114.     FatalError( "sunCG6CUpdateColormap: FBIOPUTCMAP failed\n" );
  115.     }
  116. }
  117.  
  118. /*-
  119.  *-----------------------------------------------------------------------
  120.  * sunCG6CSaveScreen --
  121.  *    Preserve the color screen by turning on or off the video
  122.  *
  123.  * Results:
  124.  *    None.
  125.  *
  126.  * Side Effects:
  127.  *    Video state is switched
  128.  *
  129.  *-----------------------------------------------------------------------
  130.  */
  131. static Bool
  132. sunCG6CSaveScreen (pScreen, on)
  133.     ScreenPtr      pScreen;
  134.     Bool          on;
  135. {
  136.     int        state = on;
  137.  
  138.     switch (on) {
  139.     case SCREEN_SAVER_FORCER:
  140.     SetTimeSinceLastInputEvent();
  141.     state = 1;
  142.     break;
  143.     case SCREEN_SAVER_OFF:
  144.     state = 1;
  145.     break;
  146.     case SCREEN_SAVER_ON:
  147.     default:
  148.     state = 0;
  149.     break;
  150.     }
  151.     (void) ioctl(sunFbs[pScreen->myNum].fd, FBIOSVIDEO, &state);
  152.     return( TRUE );
  153. }
  154.  
  155. /*-
  156.  *-----------------------------------------------------------------------
  157.  * sunCG6CCloseScreen --
  158.  *    called to ensure video is enabled when server exits.
  159.  *
  160.  * Results:
  161.  *    Screen is unsaved.
  162.  *
  163.  * Side Effects:
  164.  *    None
  165.  *
  166.  *-----------------------------------------------------------------------
  167.  */
  168. /*ARGSUSED*/
  169. static Bool
  170. sunCG6CCloseScreen(i, pScreen)
  171.     int        i;
  172.     ScreenPtr    pScreen;
  173. {
  174.     Bool    ret;
  175.     u_char rmap[256], gmap[256], bmap[256];
  176.  
  177.     pScreen->CloseScreen = (Bool (*)()) pScreen->devPrivates[sunCG6CScreenIndex].ptr;
  178.     ret = (*pScreen->CloseScreen) (i, pScreen);
  179.  
  180.     /* the following 2 lines are to fix rr clear_colormap bug */
  181.     rmap[255] = gmap[255] = bmap[255] = 0;
  182.     sunCG6CUpdateColormap(pScreen, 255, 1, rmap, gmap, bmap);
  183.  
  184.     sunCG6CInstalledMap = NULL;
  185.     (void) (*pScreen->SaveScreen) (pScreen, SCREEN_SAVER_OFF);
  186.     return ret;
  187. }
  188.  
  189. /*-
  190.  *-----------------------------------------------------------------------
  191.  * sunCG6CInstallColormap --
  192.  *    Install given colormap.
  193.  *
  194.  * Results:
  195.  *    None
  196.  *
  197.  * Side Effects:
  198.  *    Existing map is uninstalled.
  199.  *    All clients requesting ColormapNotify are notified
  200.  *
  201.  *-----------------------------------------------------------------------
  202.  */
  203. static void
  204. sunCG6CInstallColormap(cmap)
  205.     ColormapPtr    cmap;
  206. {
  207.     register int i;
  208.     register Entry *pent;
  209.     register VisualPtr pVisual = cmap->pVisual;
  210.     u_char      rmap[256], gmap[256], bmap[256];
  211.  
  212.     if (cmap == sunCG6CInstalledMap)
  213.     return;
  214.     if (sunCG6CInstalledMap)
  215.     WalkTree(sunCG6CInstalledMap->pScreen, TellLostMap,
  216.          (pointer) &(sunCG6CInstalledMap->mid));
  217.     if ((pVisual->class | DynamicClass) == DirectColor) {
  218.     for (i = 0; i < 256; i++) {
  219.         pent = &cmap->red[(i & pVisual->redMask) >>
  220.                   pVisual->offsetRed];
  221.         rmap[i] = pent->co.local.red >> 8;
  222.         pent = &cmap->green[(i & pVisual->greenMask) >>
  223.                 pVisual->offsetGreen];
  224.         gmap[i] = pent->co.local.green >> 8;
  225.         pent = &cmap->blue[(i & pVisual->blueMask) >>
  226.                    pVisual->offsetBlue];
  227.         bmap[i] = pent->co.local.blue >> 8;
  228.     }
  229.     } else {
  230.     for (i = 0, pent = cmap->red;
  231.          i < pVisual->ColormapEntries;
  232.          i++, pent++) {
  233.         if (pent->fShared) {
  234.         rmap[i] = pent->co.shco.red->color >> 8;
  235.         gmap[i] = pent->co.shco.green->color >> 8;
  236.         bmap[i] = pent->co.shco.blue->color >> 8;
  237.         }
  238.         else {
  239.         rmap[i] = pent->co.local.red >> 8;
  240.         gmap[i] = pent->co.local.green >> 8;
  241.         bmap[i] = pent->co.local.blue >> 8;
  242.         }
  243.     }
  244.     }
  245.     sunCG6CInstalledMap = cmap;
  246.     sunCG6CUpdateColormap(cmap->pScreen, 0, 256, rmap, gmap, bmap);
  247.     WalkTree(cmap->pScreen, TellGainedMap, (pointer) &(cmap->mid));
  248. }
  249.  
  250. /*-
  251.  *-----------------------------------------------------------------------
  252.  * sunCG6CUninstallColormap --
  253.  *    Uninstall given colormap.
  254.  *
  255.  * Results:
  256.  *    None
  257.  *
  258.  * Side Effects:
  259.  *    default map is installed
  260.  *    All clients requesting ColormapNotify are notified
  261.  *
  262.  *-----------------------------------------------------------------------
  263.  */
  264. static void
  265. sunCG6CUninstallColormap(cmap)
  266.     ColormapPtr    cmap;
  267. {
  268.     if (cmap == sunCG6CInstalledMap) {
  269.     Colormap defMapID = cmap->pScreen->defColormap;
  270.  
  271.     if (cmap->mid != defMapID) {
  272.         ColormapPtr defMap = (ColormapPtr) LookupIDByType(defMapID,
  273.                                   RT_COLORMAP);
  274.  
  275.         if (defMap)
  276.         (*cmap->pScreen->InstallColormap)(defMap);
  277.         else
  278.             ErrorF("sunCG6C: Can't find default colormap\n");
  279.     }
  280.     }
  281. }
  282.  
  283. /*-
  284.  *-----------------------------------------------------------------------
  285.  * sunCG6CListInstalledColormaps --
  286.  *    Fills in the list with the IDs of the installed maps
  287.  *
  288.  * Results:
  289.  *    Returns the number of IDs in the list
  290.  *
  291.  * Side Effects:
  292.  *    None
  293.  *
  294.  *-----------------------------------------------------------------------
  295.  */
  296. /*ARGSUSED*/
  297. static int
  298. sunCG6CListInstalledColormaps(pScreen, pCmapList)
  299.     ScreenPtr    pScreen;
  300.     Colormap    *pCmapList;
  301. {
  302.     *pCmapList = sunCG6CInstalledMap->mid;
  303.     return (1);
  304. }
  305.  
  306.  
  307. /*-
  308.  *-----------------------------------------------------------------------
  309.  * sunCG6CStoreColors --
  310.  *    Sets the pixels in pdefs into the specified map.
  311.  *
  312.  * Results:
  313.  *    None
  314.  *
  315.  * Side Effects:
  316.  *    None
  317.  *
  318.  *-----------------------------------------------------------------------
  319.  */
  320. static void
  321. sunCG6CStoreColors(pmap, ndef, pdefs)
  322.     ColormapPtr    pmap;
  323.     int        ndef;
  324.     xColorItem    *pdefs;
  325. {
  326.     u_char    rmap[256], gmap[256], bmap[256];
  327.     register int i;
  328.  
  329.     if (pmap != sunCG6CInstalledMap)
  330.     return;
  331.     while (ndef--) {
  332.     i = pdefs->pixel;
  333.     rmap[i] = pdefs->red >> 8;
  334.     gmap[i] = pdefs->green >> 8;
  335.     bmap[i] = pdefs->blue >> 8;
  336.     sunCG6CUpdateColormap(pmap->pScreen, i, 1, rmap, gmap, bmap);
  337.     pdefs++;
  338.     }
  339. }
  340.  
  341. /*-
  342.  *-----------------------------------------------------------------------
  343.  * sunCG6Init --
  344.  *    Attempt to find and initialize a cg6 framebuffer
  345.  *
  346.  * Results:
  347.  *    TRUE if everything went ok. FALSE if not.
  348.  *
  349.  * Side Effects:
  350.  *    Most of the elements of the ScreenRec are filled in. Memory is
  351.  *    allocated for the frame buffer and the buffer is mapped. The
  352.  *    video is enabled for the frame buffer...
  353.  *
  354.  *-----------------------------------------------------------------------
  355.  */
  356. /*ARGSUSED*/
  357. static Bool
  358. sunCG6CInit (index, pScreen, argc, argv)
  359.     int              index;        /* The index of pScreen in the ScreenInfo */
  360.     ScreenPtr      pScreen;      /* The Screen to initialize */
  361.     int              argc;            /* The number of the Server's arguments. */
  362.     char          **argv;       /* The arguments themselves. Don't change! */
  363. {
  364.     if (!cfbScreenInit (pScreen, sunFbs[index].fb,
  365.             sunFbs[index].info.fb_width,
  366.             sunFbs[index].info.fb_height,
  367.             monitorResolution, monitorResolution,
  368.             sunFbs[index].info.fb_width))
  369.         return (FALSE);
  370.  
  371.     pScreen->SaveScreen    =            sunCG6CSaveScreen;
  372.  
  373.     pScreen->devPrivates[sunCG6CScreenIndex].ptr = (pointer) pScreen->CloseScreen;
  374.     pScreen->CloseScreen = sunCG6CCloseScreen;
  375.     
  376. #ifndef STATIC_COLOR
  377.     pScreen->InstallColormap = sunCG6CInstallColormap;
  378.     pScreen->UninstallColormap = sunCG6CUninstallColormap;
  379.     pScreen->ListInstalledColormaps = sunCG6CListInstalledColormaps;
  380.     pScreen->StoreColors = sunCG6CStoreColors;
  381. #endif
  382.  
  383.     sunCG6CSaveScreen( pScreen, SCREEN_SAVER_FORCER );
  384.     return sunScreenInit (pScreen) &&  cfbCreateDefColormap(pScreen);
  385. }
  386.  
  387. /*-
  388.  *--------------------------------------------------------------
  389.  * sunCG6CSwitch --
  390.  *      Enable or disable color plane 
  391.  *
  392.  * Results:
  393.  *      Color plane enabled for select =0, disabled otherwise.
  394.  *
  395.  *--------------------------------------------------------------
  396.  */
  397. static void
  398. /*ARGSUSED*/
  399. sunCG6CSwitch (pScreen, select)
  400.     ScreenPtr    pScreen;
  401.     int        select;
  402. {
  403. }
  404.  
  405. /*-
  406.  *-----------------------------------------------------------------------
  407.  * sunCG6Probe --
  408.  *    Attempt to find and initialize a cg6 framebuffer
  409.  *
  410.  * Results:
  411.  *    TRUE if everything went ok. FALSE if not.
  412.  *
  413.  * Side Effects:
  414.  *    Memory is allocated for the frame buffer and the buffer is mapped.
  415.  *
  416.  *-----------------------------------------------------------------------
  417.  */
  418. Bool
  419. sunCG6CProbe (pScreenInfo, index, fbNum, argc, argv)
  420.     ScreenInfo      *pScreenInfo;    /* The screenInfo struct */
  421.     int              index;        /* The index of pScreen in the ScreenInfo */
  422.     int              fbNum;        /* Index into the sunFbData array */
  423.     int              argc;            /* The number of the Server's arguments. */
  424.     char          **argv;       /* The arguments themselves. Don't change! */
  425. {
  426.     int         fd;
  427.     struct fbtype fbType;
  428.     int        pagemask, mapsize;
  429.     int        imagelen;
  430.     caddr_t    mapaddr;
  431.     caddr_t    addr;
  432. #ifdef sprite
  433.     int        sizeToUse;
  434. #endif /* sprite */
  435.  
  436.     if ((fd = sunOpenFrameBuffer(FBTYPE_SUNFAST_COLOR, &fbType, index, fbNum,
  437.                  argc, argv)) < 0)
  438.     return FALSE;
  439.  
  440.     pagemask = getpagesize() - 1;
  441.     imagelen = fbType.fb_width * fbType.fb_height;
  442.     mapsize = (imagelen + pagemask) & ~pagemask;
  443.     addr = 0;
  444.  
  445. #ifndef    _MAP_NEW
  446. #ifndef sprite
  447.     addr = (caddr_t) valloc(mapsize);
  448.     if (addr == (caddr_t) NULL) {
  449.     ErrorF("Could not allocate room for frame buffer.\n");
  450.     (void) close (fd);
  451.     return FALSE;
  452.     }
  453. #endif /* sprite */
  454. #endif    _MAP_NEW
  455.  
  456. #ifdef sprite
  457.     sizeToUse = ((mapsize + VMMACH_SEG_SIZE) & ~(VMMACH_SEG_SIZE-1))
  458.         + VMMACH_SEG_SIZE;
  459.     addr = (caddr_t) malloc(sizeToUse);
  460.     if (addr == (caddr_t) NULL) {
  461.     ErrorF("Could not allocate room for frame buffer.\n");
  462.     return FALSE;
  463.     }
  464.     addr = (caddr_t) mmap((caddr_t) addr,
  465.          mapsize,
  466.          PROT_READ | PROT_WRITE,
  467.          MAP_SHARED, fd, 0);
  468.     if (addr == (caddr_t) NULL) {
  469. #else
  470.     mapaddr = (caddr_t) mmap((caddr_t) addr,
  471.          mapsize,
  472.          PROT_READ | PROT_WRITE,
  473.          MAP_SHARED, fd, CG6_VADDR_COLOR);
  474.  
  475.  
  476.     if (mapaddr == (caddr_t) -1) {
  477. #endif /* sprite */
  478.     Error("Mapping cg6c");
  479.     (void) close(fd);
  480.     return FALSE;
  481.     }
  482.  
  483. #ifndef sprite
  484.     if (mapaddr == 0)
  485.         mapaddr = addr;
  486. #endif /* sprite */
  487.  
  488.     sunFbs[index].info = fbType;
  489. #ifndef sprite
  490.     sunFbs[index].fb = (pointer) fba.fb_buffer;
  491. #else
  492.     sunFbs[index].fb = (pointer) addr;
  493. #endif /* sprite */
  494.     sunFbs[index].EnterLeave = sunCG6CSwitch;
  495.     sunSupportsDepth8 = TRUE;
  496.     sunFbs[index].fd = fd;
  497.     return TRUE;
  498. }
  499.  
  500.  
  501. Bool
  502. sunCG6CCreate(pScreenInfo, argc, argv)
  503.     ScreenInfo      *pScreenInfo;
  504.     int              argc;
  505.     char          **argv;
  506. {
  507.     int i;
  508.  
  509.     if (sunGeneration != serverGeneration)
  510.     {
  511.     sunCG6CScreenIndex = AllocateScreenPrivateIndex();
  512.     if (sunCG6CScreenIndex < 0)
  513.         return FALSE;
  514.     }
  515.     i = AddScreen(sunCG6CInit, argc, argv);
  516.     if (i >= 0)
  517.     return TRUE;
  518.     return FALSE;
  519. }
  520.